<template>
  <div class="home_page">

    <h2>分类列表</h2>
    <div class="tagbox">
      <button v-for="type in typeList" :key="type.id" @click="handleSeachType(type.id)">
        {{ type.name }}
      </button>
    </div>
    
    <h2>标签列表</h2>
    <div class="tagbox">
      <button v-for="tag in tagList" :key="tag.id" @click="handleSeachTag(tag.id)">
        {{ tag.tname }}
      </button>
    </div>

    <div class="article_list">
      <div v-for="article in articleList" class="article_card" :key="article.id" >
        <div @click="handleArticle(article.id)">
          <div class="thumbnail">
            <img :src="article.picture"/>
          </div>
          <div class="info">
            <div class="title">{{ article.title }}</div>
            <div class="meta">
              <span>👁 {{ article.views }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>
  
<script setup>
import { ref, onMounted } from 'vue'
import { homePageRecommend } from '@/api/BlogVisit'
import { useRouter } from 'vue-router'
import { searchArtByType } from '@/api/BlogVisit'
import { searchArtByTag } from '@/api/BlogVisit'
import hahaPng from '@/assets/1234.jpg'
import server from '@/api/server'


const handleSeachType = (typeId)=>{
  const params = {
    typeId: typeId
  }
  try {
    searchArtByType(params).then((res)=>{
      if (res.data.code==0) {
        server.searchMsg = "根据文章类型搜索："
        server.searchData = res.data
        router.push('/articleList')
      } else {
        alert("查找失败："+res.data.msg)
      }
    })
  } catch (error) {
    alert("查找失败："+error)
  }
}


const handleSeachTag = (tagId)=>{
  const params = {
    tagId: tagId
  }
  try {
    searchArtByTag(params).then((res)=>{
      if (res.data.code==0) {
        server.searchMsg = "根据文章标签搜索："
        server.searchData = res.data
        router.push('/articleList')
      } else {
        alert("查找失败："+res.data.msg)
      }
    })
  } catch (error) {
    alert("查找失败："+error)
  }
}

const router = useRouter();

const typeList = ref([{
  id: 0,
  name: '专栏'
}])

const tagList = ref([{
  id: 0,
  tname: '番剧'
}])

const articleList = ref([{
  id: 1,
  userId: 1,
  picture: hahaPng,
  title: 'hahahaha',
  views: 999
}])

onMounted(()=>{
  getData()
})

const getData = () =>{
  homePageRecommend().then((res)=>{
    if(res.data.code == 0){
      console.log(res.data.data);
      articleList.value = res.data.data.articlePage.list;
      tagList.value = res.data.data.articleTag;
      typeList.value = res.data.data.articleType;
    }else{

      alert("首页加载失败："+res.data.msg);

    }
  })
}

const handleArticle = (articleId)=>{
  server.currentPageId = articleId;
  router.push("/detail");
}
</script>



<style scoped>
.home_page {
  width: 1000px;
  margin: 0 auto;padding: 25px;
  background: #f4f4f4;
}

.tagbox {
  display: flex;flex-flow:row wrap;
}

.tagbox button{
  display: flex;flex-flow:row wrap;
  justify-content: center;
  width: 70px;height: 40px;margin-right: 15px;

  align-items: center;
  background: white;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  transition: background 0.2s;
}

.tagbox button:hover{
  background: #e5e7eb;
}






/* 文章列表 */
.article_list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin: 20px 0vw 120px;
}
/* 文章卡片 */
.article_card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 0 6px rgba(0,0,0,0.05);
  transition: transform 0.2s;
}

.article_card:hover {
  transform: translateY(-4px);
}

/* 缩略图 */
.thumbnail {
  height: 180px;
  overflow: hidden;
  position: relative;
}

.thumbnail img {
  height: 100%;width: 100%;
  object-fit: cover;
}

/* 文章信息 */
.info {
  padding: 8px 10px;
}

.info .title {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.info .meta {
  font-size: 12px;
  color: #999;
}
</style>